package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.functiongraph.v2.FunctionGraphClient;
import com.huaweicloud.sdk.functiongraph.v2.model.AsyncInvokeFunctionResponse;
import com.huaweicloud.sdk.functiongraph.v2.model.InvokeFunctionResponse;
import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion;
import com.huaweicloud.sdk.functiongraph.v2.model.AsyncInvokeFunctionRequest;
import com.huaweicloud.sdk.functiongraph.v2.model.InvokeFunctionRequest;

import java.util.Map;
import java.util.HashMap;

public class FunctionInvokeDemo {

    public static void main(String[] args) {
        FunctionGraphClient client = buildFunctionClient();

        // 函数urn可以在函数创建后的详情页面查看
        String urn = "urn:fss:<region>:<projectId>:function:default:<functionName>:<version>";
        Map<String, Object> invokeFunctionRequestBody = new HashMap<>();
        invokeFunctionRequestBody.put("key", "value");
        // 同步请求
        InvokeFunctionRequest syncRequest = new InvokeFunctionRequest();
        syncRequest.withFunctionUrn(urn);
        syncRequest.withBody(invokeFunctionRequestBody);
        // 异步请求
        AsyncInvokeFunctionRequest asyncRequest = new AsyncInvokeFunctionRequest();
        asyncRequest.withFunctionUrn(urn);
        asyncRequest.withBody(invokeFunctionRequestBody);
        try {
            // 同步调用
            InvokeFunctionResponse syncResponse = client.invokeFunction(syncRequest);
            System.out.println(syncResponse.toString());
            // 异步调用
            AsyncInvokeFunctionResponse response = client.asyncInvokeFunction(asyncRequest);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    static FunctionGraphClient buildFunctionClient() {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
        // 根据需要配置网络代理
        HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig();
        httpConfig.setProxyHost("<host>");
        httpConfig.setProxyPort(80);
        httpConfig.setProxyUsername("<user>");
        httpConfig.setProxyPassword("<password>");

        return FunctionGraphClient.newBuilder()
                .withCredential(auth)
                .withRegion(FunctionGraphRegion.valueOf("<region>"))
                .build();
    }
}
